<!DOCTYPE html>
<html>
<head>
    <title>血糖测量</title>
	<titleType style="display: none">healthTest</titleType>
	<%- include('../../_global/baseLibs') %>
	<%- include('./measurement') %>
<style type="text/css">
	.border {
		border: 1px solid #40BDFF !important;
	}
</style>
</head>

<body>

<!-- header start -->
<header id="header" class="header-box grey fixed"  >
	<div class="header-content">
		<div class="left-tool">
			<a class="back" href="javascript:void(0)"><i class="iconfont icon-back"></i></a>
		</div>
		<div class="title">血糖测量</div>
		<div class="right-tool">
			<a class="tool-a" >提交</a>
		</div>
	</div>
</header>
<!-- header end -->
	<div class="mui-content">
		<div class="background">
			<!-- 头部导航栏 -->
			<div class="topBar">
				<div class="text">
					<a id="id_a" onclick="changeTime()">
						<img alt="" src="/static/images/back.png">
						<span class="title" >测量时间</span>
						<span id="testTime" class="time" ></span>
					</a>
				</div>
				<div class="line"></div>
			</div>

			<!-- 标尺块 -->
			<div id="xt-box" class='box'>
				<span class="title" >血糖</span>
				<div class='valueplus'>
					<div class='minus'>
						<a>
							<img id="minus_img" src="/static/images/measurement/selfTest/minus.png">
						</a>
					</div>
					<div class='value'>
						<span id='value'>0</span>
						<span id='unit'>mmol/L</span>
					</div>
					<div class='plus'>
						<a>
							<img id="add_img" src="/static/images/measurement/selfTest/add.png">
						</a>
					</div>
				</div>
				<div id='rulerWrapper'></div>
			</div>

			<!-- 时间点 -->
			<div class="TimeBox">
				<span class="title" >时间点</span>
				<div class="spanBox">
				</div>
			</div>
		</div>
	</div>
			<button id="titleBtn"   style="width: 100%;height:40px;color: #3082FF;position: fixed;bottom: 0px;;display: none;">提交</button>

<script type="text/javascript">

    var page_sugerMobile = (function($, page_sugerMobile) {


        //尺子蓝色区域
        var greenLeft = 4.4;
        var greenRight = 10.0;

        /*血糖类型*/
        var cllx = '01';

        /*
		 * 提交血糖测量数据
		 */
        function submitSuger(){

            if(page_sugerMobile.validate()) {
                var data = page_sugerMobile.getData();
                $.QuickRemote.AjaxJson({
                    funcName: uri_service_tzcl_save
                    , params: data
                    , callback: function (result) {
                        $.QuickAlert.alertNormal("保存成功", {
                            callback: function(){
                                var url =$.getUrl.pageUrl("measurement","selfTest_mobile");
                                var data = {refreshUrl:url,pageUrl:url};
                                $.QuickUrlUtils.refreshJSBridge(data);
                                $.pageSwitch.goToUrl("measurement","records_mobile","cllx=" + cllx);
                            }
                        })
                    }
                });
            }
        }

        function getData(){
            /*测量时间*/
            var clsj = $("#testTime").text();
            /*时间点*/
            var sjd = $(".timeSpan[class*='border']").data("data")["value"];
            /*血糖*/
            var xt = $("#value").text();
            return {
                cllx: cllx
                ,clsj: clsj
                ,sjd: sjd
                ,xt: xt
            };
        }

        function validate(){
            var div = $(".timeSpan[class*='border']");
            if(div['length'] == 0){
                $.QuickAlert.alertFail({
                    content: '请选择时间点'
                });
                return false;
            }
            return true;
        }

        /*时间段点击回调*/
        function sjdClickHandler(){
            $(this).addClass("border").siblings().removeClass("border").find('p').css('color', '#8f8f94');
            $(this).find('p').css('color', '#40BDFF');

            var data = $(this).data("data");
            var value = data['value'];
            if(value == '0' || value == '2' || value == '4' || value == '6' || value == '7'){
                greenRight = 10.0;
                sugerRuler.setblueColor(4.4,10.0);
            }
            if(value == '1' || value == '3' || value == '5'){
                greenRight = 7.0;
                sugerRuler.setblueColor(4.4,7.0);
            }
            page_sugerMobile.rulerSetValue(parseFloat($("#value").text()));
        }

        /*创建时间段item*/
        function createSjdItem(){
            var ary = $.JkcsZydmUtils.getAry({
                tableName: 'tzcl_sjd'
            });
            $.each(ary, function(index, item){
                var sjd_item = $($("#sjd_item").html());
                sjd_item.data("data", item);
                sjd_item.find("p").text(item['text']);
                $(".spanBox").append(sjd_item);
            })
        }

        //回调函数 改变数值和颜色
        function rulerSetValue(val){
            $("#value").html(val);
            if(val >= greenLeft && val <= greenRight ){
                $('.valueplus .value span').css("color","#40BDFF");
            }else{
                $('.valueplus .value span').css("color","#FDB178");
            }
        }

        var page_sugerMobile = {
            submitSuger: submitSuger,
            getData: getData,
            validate: validate,
            sjdClickHandler: sjdClickHandler,
            createSjdItem: createSjdItem,
            rulerSetValue: rulerSetValue,
        }

        //初始化尺子
        var cfg_def = new MeasureRuler({
            wrapperId:"rulerWrapper",
            max:35,
            minUnit:0.1,// 每个刻度代表的值
            unitSet:10, // 每组刻度个数
            startValue : 1,
            mult:1, //10px
            value:6.0,
            greenLeft : greenLeft,
            greenRight : greenRight,
            callback: page_sugerMobile.rulerSetValue
        });

        mui.init({
            swipeBack: false //启用右滑关闭功能
        });

        page_sugerMobile.init = function(cfg){
            $.extend(page_sugerMobile, cfg_def, cfg);
            var u = navigator.userAgent.toLowerCase();
//            if( u.indexOf('micromessenger') != -1){
//                $("#titleBtn").show();
//            }


            initRule("xt-box", sugerRuler, 0.1);
            //时间段点击
            $(".spanBox").on ("click", '.timeSpan', page_sugerMobile.sjdClickHandler);
            $(".tool-a").on ("click", page_sugerMobile.submitSuger);
            $("#titleBtn").on ("click", page_sugerMobile.submitSuger);


            page_sugerMobile.createSjdItem();

            var date = new Date();
            $('#testTime').text(date.Format('yyyy-MM-dd HH:mm:ss'));
//        picker.setSelectedValue(tet);

        }

        return page_sugerMobile;
    })(jQuery, {});




</script>

</body>
<script type="text/html" id="sjd_item">
    <div class="timeSpan">
        <p></p>
	</div>
</script>
</html>